package cn.guozeyiblog.blog;

import java.util.Properties;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class Feedback extends Activity implements OnClickListener{

	private TextView tv_subject;
	private TextView tv_content;
	private Button sendmsg;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.feed);
		initView();
		sendmsg.setOnClickListener(this);
	}

	public void initView(){
		tv_content = (TextView) findViewById(R.id.textView2);
		tv_subject = (TextView) findViewById(R.id.textView1);
		sendmsg = (Button) findViewById(R.id.feed_button);
	}

	@SuppressLint("NewApi")
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		String subjectString = tv_subject.getText().toString();
		String contentString = tv_content.getText().toString();
		if (!contentString.isEmpty()) {
			//发送邮件
            send("*****", "*****", subjectString, contentString);
		}
	}
	public void send(String from, String to, String subject, String content) {
		Properties props = new Properties();
		props.put("mail.smtp.host", "smtp.163.com"); // 指定SMTP服务器
		props.put("mail.smtp.auth", "true"); // 指定是否需要SMTP验证
		try {
			Session mailSession = Session.getDefaultInstance(props);
			Message message = new MimeMessage(mailSession);
			message.setFrom(new InternetAddress(from,"发件人名称")); // 发件人
			message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); // 收件人
			message.setSubject(subject); // 邮件主题
			message.setContent(content, "text/html;charset=utf-8");//邮件内容包含html
			//message.setText(content); // 邮件内容纯文本
			message.saveChanges();
			Transport transport = mailSession.getTransport("smtp");
			transport.connect("smtp.163.com", "********", "********");
			transport.sendMessage(message, message.getAllRecipients());
			transport.close();
		} catch(Exception e) {
			System.out.println(e);
		}
	}

}